package mes.kanban.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mes.kanban.entity.MesConfigList;
import mes.kanban.service.ConfigListI;
import mes.kanban.service.PollingConfigServiceI;
import mes.kanban.util.IsNotNullValit;
import mes.kanban.util.PageBean;
import mes.kanban.vo.AjaxReturnVO;
import mes.kanban.vo.MesPollingConfigVO;
import mes.kanban.vo.MesUserVO;

/**
 * 配置控制类
 * @author gjy
 *
 * 2018年2月11日下午8:14:47
 */
@Controller
@RequestMapping(value="/pollingConfig")
public class PollingConfigController {
	
	
	private static final Logger log = LoggerFactory.getLogger(PollingConfigController.class);

	
	@Autowired
	private PollingConfigServiceI pollingConfigService;
	
	@Autowired
	private ConfigListI configService;
	
	/**
	 * 跳转到看板列表页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/toList")
    public String toList(Model model,HttpServletRequest request) {
        return "pollingConfig/list";
    }
	
	/**
	 * 查询看板列表数据
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value="/list")
	@ResponseBody
    public AjaxReturnVO list(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize,HttpServletRequest request) {
		
		log.info("查询看板配置信息");
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			HttpSession session = WebUtils.toHttp(request).getSession();
			Object info = session.getAttribute("userSession");
			MesUserVO userInfo = new MesUserVO();
			BeanUtils.copyProperties(info, userInfo);
			PageBean configs = pollingConfigService.findPollingConfigForPageBean(pageNum,pageSize,userInfo.getId());
			vo.setData(configs);
			vo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("请求失败");
			vo.setOk(false);
		}
		return vo;
    }
	
	/**
	 * 添加看板信息
	 * @param pollingConfig
	 * @return
	 */
    @PostMapping("/add")
    @ResponseBody
    public AjaxReturnVO add(@RequestBody MesPollingConfigVO pollingConfig) {
    	log.info("添加看板配置信息");
		AjaxReturnVO ajaxVo = new AjaxReturnVO();
		try {
			pollingConfigService.save(pollingConfig);
	    	ajaxVo.setData(pollingConfig);
	    	ajaxVo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxVo.setMsg("请求失败");
			ajaxVo.setOk(false);
		}
    	return ajaxVo;
    }

    /**
     * 编辑看板信息
     * @param vo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxReturnVO edit(@RequestBody MesPollingConfigVO vo) {
    	log.info("修改看板配置信息");
		AjaxReturnVO ajaxVo = new AjaxReturnVO();
		try {
	    	pollingConfigService.edit(vo);
	    	ajaxVo.setData(vo);
	    	ajaxVo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxVo.setMsg("请求失败");
			ajaxVo.setOk(false);
		}
    	return ajaxVo;
    }
    
    /**
     * 删除看板信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public AjaxReturnVO deleteById(@RequestParam("id") String id) {
    	AjaxReturnVO j = new AjaxReturnVO();
    	try {
			j.setOk(true);
			List<MesConfigList> cs = configService.getConfigsByPolingConfigId(id);
	    	if(IsNotNullValit.CollectionIsNotNull(cs)) {
	    		pollingConfigService.deletes(id,cs);
	    	}else {
	    		pollingConfigService.deleteById(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
    	return j;
    }
    
    /**
     * 根据看板Id查询详细配置列表
     * @param pollingId
     * @return
     */
    @GetMapping(value="/findConfigByPollingId")
    @ResponseBody
    public AjaxReturnVO findConfigByPollingId(@RequestParam("id")String pollingId) {
    	
    	AjaxReturnVO j = new AjaxReturnVO();
    	
    	try {
			j.setOk(true);
			
			List<MesConfigList> cs = configService.getConfigsByPolingConfigId(pollingId);
			
			if(IsNotNullValit.CollectionIsNotNull(cs)) {
				j.setMsg("have");
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
    	return j;
    }
    
    /**
     * 保存看板字体配置
     * @param pollingId
     * @return
     */
    @GetMapping(value="/saveFontSizeConfig")
    @ResponseBody
    public AjaxReturnVO fontSizeConfig(MesPollingConfigVO vo) {
    	
    	AjaxReturnVO j = new AjaxReturnVO();
    	
    	try {
    		pollingConfigService.edit(vo);
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
    	return j;
    }
    
}
